//
//  YBTransferInEarnView.m
//  Nectar
//
//  Created by famer on 2023/11/10.
//  Copyright © 2023 asance. All rights reserved.
//

#import "YBTransferInEarnView.h"
#import "RevDataGetTransferInEarn.h"
#import "RevDataGetRechargeIncomeConfig.h"

NSString *const LEVEL_BRONZE = @"BRONZE";
NSString *const LEVEL_GOLD = @"GOLD";
NSString *const LEVEL_PLATINUM = @"PLATINUM";
NSString *const LEVEL_DIAMOND = @"DIAMOND";

@implementation YBTransferInEarnView

- (id)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if(self){
        _process = 0.1;
        _levelType = LEVEL_BRONZE;
        
        [self addSubview:self.currentLevelLabel];
        [self addSubview:self.levelTypeLabel];
        [self addSubview:self.levelTypeImageView];
        
        [self addSubview:self.currentEarnTodayLabel];
        [self addSubview:self.currentEarnTodayValueLabel];
        [self addSubview:self.currentEarnTodayUnitLabel];
        
        [self addSubview:self.earnRatioLabel];
        [self addSubview:self.earnRatioValueLabel];
        
        [self addSubview:self.withdrawalTimesLabel];
        [self addSubview:self.withdrawalTimesValueLabel];
        
        [self addSubview:self.processContentView];
        [self.processContentView addSubview:self.processView];
        [self addSubview:self.maxProcessValueLabel];
        [self addSubview:self.maxProcessUnitLabel];
        
        [self addSubview:self.maxSingleTransactionLabel];
        [self addSubview:self.maxSingleTransactionValueLabel];
        [self addSubview:self.maxSingleTransactionUnitLabel];
        
        [self addSubview:self.actionButton];

        self.levelTypeImageView.contentMode = UIViewContentModeScaleAspectFit;
        
        self.currentLevelLabel.hidden = YES;

        self.processContentView.clipsToBounds = YES;
        self.processContentView.layer.cornerRadius = 5;
        
        self.processView.clipsToBounds = YES;
        self.processView.layer.cornerRadius = 4;
        
        self.currentLevelLabel.backgroundColor = [UIColor hexColor:@"FFFFFF" alpha:0.5];
        self.currentLevelLabel.clipsToBounds = YES;
        self.currentLevelLabel.layer.cornerRadius = 2;
        
    }
    return self;
}

- (void)layoutSubviews{
    [super layoutSubviews];
    
    CGFloat width = self.frame.size.width;
    CGFloat leftMargin = 12;
    CGFloat topMargin = 16;
    
    CGSize currentLevelLabelSize = [self.currentLevelLabel textBoundingSizeWithMaxSize:CGSizeMake(400, 500)];
    self.currentLevelLabel.frame = CGRectMake(leftMargin,
                                              6,
                                              currentLevelLabelSize.width+4,
                                              currentLevelLabelSize.height);
    
    CGSize levelTypeLabelSize = [self.levelTypeLabel textBoundingSizeWithMaxSize:CGSizeMake(400, 500)];
    self.levelTypeLabel.frame = CGRectMake(width-levelTypeLabelSize.width-leftMargin,
                                           topMargin,
                                           levelTypeLabelSize.width,
                                           levelTypeLabelSize.height);
    self.levelTypeImageView.frame = CGRectMake(width-60-14, self.levelTypeLabel.bottom+14, 60, 60);
    
    CGSize currentEarnTodayLabelSize = [self.currentEarnTodayLabel textBoundingSizeWithMaxSize:CGSizeMake(400, 500)];
    self.currentEarnTodayLabel.frame = CGRectMake(leftMargin,
                                                  self.levelTypeLabel.bottom,
                                                  currentEarnTodayLabelSize.width,
                                                  currentEarnTodayLabelSize.height);
    
    CGSize currentEarnTodayUnitLabelSize = [self.currentEarnTodayUnitLabel textBoundingSizeWithMaxSize:CGSizeMake(400, 500)];
    self.currentEarnTodayUnitLabel.frame = CGRectMake(self.levelTypeImageView.left-currentEarnTodayUnitLabelSize.width-leftMargin,
                                                      self.currentEarnTodayLabel.top,
                                                      currentEarnTodayUnitLabelSize.width,
                                                      currentEarnTodayUnitLabelSize.height);
    
    CGSize currentEarnTodayValueLabelSize = [self.currentEarnTodayValueLabel textBoundingSizeWithMaxSize:CGSizeMake(400, 500)];
    self.currentEarnTodayValueLabel.frame = CGRectMake(self.currentEarnTodayUnitLabel.left-currentEarnTodayValueLabelSize.width,
                                                       self.currentEarnTodayLabel.top,
                                                       currentEarnTodayValueLabelSize.width,
                                                       currentEarnTodayValueLabelSize.height);
    
    self.currentEarnTodayLabel.centerY = self.currentEarnTodayValueLabel.centerY;
    self.currentEarnTodayUnitLabel.centerY = self.currentEarnTodayValueLabel.centerY;
    
    
    ///
    CGSize earnRatioValueLabelSize = [self.earnRatioValueLabel textBoundingSizeWithMaxSize:CGSizeMake(400, 500)];
    self.earnRatioValueLabel.frame = CGRectMake(self.currentEarnTodayUnitLabel.right-earnRatioValueLabelSize.width,
                                                self.currentEarnTodayValueLabel.bottom+10,
                                                earnRatioValueLabelSize.width,
                                                earnRatioValueLabelSize.height);
    
    CGSize earnRatioLabelSize = [self.earnRatioLabel textBoundingSizeWithMaxSize:CGSizeMake(400, 500)];
    self.earnRatioLabel.frame = CGRectMake(self.currentEarnTodayLabel.left,
                                           self.earnRatioValueLabel.top,
                                           earnRatioLabelSize.width,
                                           earnRatioLabelSize.height);
    
    self.earnRatioLabel.centerY = self.earnRatioValueLabel.centerY;
    
    ///
    CGSize withdrawalTimesValueLabelSize = [self.withdrawalTimesValueLabel textBoundingSizeWithMaxSize:CGSizeMake(400, 500)];
    self.withdrawalTimesValueLabel.frame = CGRectMake(self.currentEarnTodayUnitLabel.right-withdrawalTimesValueLabelSize.width,
                                                      self.earnRatioValueLabel.bottom+10,
                                                      withdrawalTimesValueLabelSize.width,
                                                      withdrawalTimesValueLabelSize.height);
    
    CGSize withdrawalTimesLabelSize = [self.withdrawalTimesLabel textBoundingSizeWithMaxSize:CGSizeMake(400, 500)];
    self.withdrawalTimesLabel.frame = CGRectMake(self.currentEarnTodayLabel.left,
                                                 self.withdrawalTimesValueLabel.top,
                                                 withdrawalTimesLabelSize.width,
                                                 withdrawalTimesLabelSize.height);
    
    self.withdrawalTimesLabel.centerY = self.withdrawalTimesValueLabel.centerY;
    
    
    ///
    CGSize maxProcessUnitLabelSize = [self.maxProcessUnitLabel textBoundingSizeWithMaxSize:CGSizeMake(400, 500)];
    self.maxProcessUnitLabel.frame = CGRectMake(self.levelTypeImageView.right-maxProcessUnitLabelSize.width,
                                                self.withdrawalTimesValueLabel.bottom+16,
                                                maxProcessUnitLabelSize.width,
                                                maxProcessUnitLabelSize.height);
    
    CGSize maxProcessValueLabelSize = [self.maxProcessValueLabel textBoundingSizeWithMaxSize:CGSizeMake(400, 500)];
    self.maxProcessValueLabel.frame = CGRectMake(self.maxProcessUnitLabel.left-maxProcessValueLabelSize.width,
                                                 self.maxProcessUnitLabel.top,
                                                 maxProcessValueLabelSize.width,
                                                 maxProcessValueLabelSize.height);
    
    self.processContentView.frame = CGRectMake(leftMargin,
                                               self.maxProcessValueLabel.top,
                                               self.maxProcessValueLabel.left-16-leftMargin,
                                               10);
    
    self.processView.frame = CGRectMake(1,
                                        1,
                                        (self.processContentView.width-2)*self.process,
                                        8);
    
    self.maxProcessUnitLabel.centerY = self.maxProcessValueLabel.centerY;
    self.processContentView.centerY = self.maxProcessValueLabel.centerY;
    
    ///
    CGSize maxSingleTransactionUnitLabelSize = [self.maxSingleTransactionUnitLabel textBoundingSizeWithMaxSize:CGSizeMake(400, 500)];
    self.maxSingleTransactionUnitLabel.frame = CGRectMake(self.maxProcessUnitLabel.right-maxSingleTransactionUnitLabelSize.width,
                                                          self.maxProcessValueLabel.bottom+10,
                                                          maxSingleTransactionUnitLabelSize.width,
                                                          maxSingleTransactionUnitLabelSize.height);
    
    CGSize maxSingleTransactionValueLabelSize = [self.maxSingleTransactionValueLabel textBoundingSizeWithMaxSize:CGSizeMake(400, 500)];
    self.maxSingleTransactionValueLabel.frame = CGRectMake(self.maxSingleTransactionUnitLabel.left-maxSingleTransactionValueLabelSize.width,
                                                           self.maxSingleTransactionUnitLabel.top,
                                                           maxSingleTransactionValueLabelSize.width,
                                                           maxSingleTransactionValueLabelSize.height);
    
    CGSize maxSingleTransactionLabelSize = [self.maxSingleTransactionLabel textBoundingSizeWithMaxSize:CGSizeMake(400, 500)];
    self.maxSingleTransactionLabel.frame = CGRectMake(leftMargin,
                                                      self.maxSingleTransactionValueLabel.top,
                                                      maxSingleTransactionLabelSize.width,
                                                      maxSingleTransactionLabelSize.height);
    
    self.maxSingleTransactionLabel.centerY = self.maxSingleTransactionValueLabel.centerY;
    self.maxSingleTransactionUnitLabel.centerY = self.maxSingleTransactionValueLabel.centerY;
    
    self.actionButton.frame = self.bounds;
    
    [self updateUI];
}

- (void)updateUI{
    self.clipsToBounds = YES;
    self.layer.cornerRadius = 10;
    
    self.currentLevelLabel.clipsToBounds = YES;
    self.currentLevelLabel.layer.cornerRadius = 4;
    self.currentLevelLabel.backgroundColor = [UIColor hexColor:@"FFFFFF" alpha:0.8];
    
    self.levelTypeLabel.text =self.levelType;

    CAGradientLayer *gradientLayer = [CAGradientLayer layer];
    gradientLayer.frame = self.bounds;
    
    if([self.levelType isEqualToString:LEVEL_BRONZE]){
        UIColor *endColor = [UIColor hexColor:@"61A49C"];
        UIColor *startColor = [UIColor hexColor:@"86BEC4"];
        gradientLayer.colors = @[(id)startColor.CGColor, (id)endColor.CGColor];
        
        self.levelTypeImageView.image = [UIImage imageNamed:@"icon_earn_level_1"];
        self.processView.backgroundColor = [UIColor hexColor:@"61A49C"];
        
    } else if([self.levelType isEqualToString:LEVEL_GOLD]){
        UIColor *endColor = [UIColor hexColor:@"B8A06D"];
        UIColor *startColor = [UIColor hexColor:@"CDBC85"];
        gradientLayer.colors = @[(id)startColor.CGColor, (id)endColor.CGColor];
        
        self.levelTypeImageView.image = [UIImage imageNamed:@"icon_earn_level_2"];
        self.processView.backgroundColor = [UIColor hexColor:@"B8A06D"];
        
    } else if([self.levelType isEqualToString:LEVEL_PLATINUM]){
        UIColor *endColor = [UIColor hexColor:@"6376A8"];
        UIColor *startColor = [UIColor hexColor:@"7D87B4"];
        gradientLayer.colors = @[(id)startColor.CGColor, (id)endColor.CGColor];
        
        self.levelTypeImageView.image = [UIImage imageNamed:@"icon_earn_level_3"];
        self.processView.backgroundColor = [UIColor hexColor:@"6376A8"];
        
    } else if([self.levelType isEqualToString:LEVEL_DIAMOND]){
        UIColor *endColor = [UIColor hexColor:@"A985EF"];
        UIColor *startColor = [UIColor hexColor:@"C397F4"];
        gradientLayer.colors = @[(id)startColor.CGColor, (id)endColor.CGColor];
        
        self.levelTypeImageView.image = [UIImage imageNamed:@"icon_earn_level_4"];
        self.processView.backgroundColor = [UIColor hexColor:@"A985EF"];
    }
    
    gradientLayer.startPoint = CGPointMake(0, 0);
    gradientLayer.endPoint = CGPointMake(1, 1);
    [self.layer insertSublayer:gradientLayer atIndex:0];
}

- (void)setLevelType:(NSString *)levelType{
    _levelType = levelType;
}

- (CGFloat)getViewHeightFromSpecialWidth:(CGFloat)width{
    
    CGFloat leftMargin = 12;
    CGFloat topMargin = 16;
    
    CGFloat totalHeight = 0;
    
    // levelTypeLabelSize
    CGSize levelTypeLabelSize = [self.levelTypeLabel textBoundingSizeWithMaxSize:CGSizeMake(400, 500)];
    totalHeight+=topMargin;
    totalHeight+=levelTypeLabelSize.height;
    
    // currentEarnTodayValueLabelSize
    CGSize currentEarnTodayValueLabelSize = [self.currentEarnTodayValueLabel textBoundingSizeWithMaxSize:CGSizeMake(400, 500)];
    totalHeight+=10;
    totalHeight+=currentEarnTodayValueLabelSize.height;
    
    CGSize earnRatioValueLabelSize = [self.earnRatioValueLabel textBoundingSizeWithMaxSize:CGSizeMake(400, 500)];
    totalHeight+=10;
    totalHeight+=earnRatioValueLabelSize.height;
    
    
    ///
    CGSize withdrawalTimesValueLabelSize = [self.withdrawalTimesValueLabel textBoundingSizeWithMaxSize:CGSizeMake(400, 500)];
    totalHeight+=10;
    totalHeight+=withdrawalTimesValueLabelSize.height;
    
    ///
    CGSize maxProcessValueLabelSize = [self.maxProcessValueLabel textBoundingSizeWithMaxSize:CGSizeMake(400, 500)];
    totalHeight+=16;
    totalHeight+=maxProcessValueLabelSize.height;
    
    ///
    CGSize maxSingleTransactionValueLabelSize = [self.maxSingleTransactionValueLabel textBoundingSizeWithMaxSize:CGSizeMake(400, 500)];
    totalHeight+=10;
    totalHeight+=maxSingleTransactionValueLabelSize.height;
    
    totalHeight+=topMargin;
    
    return totalHeight;
}

- (void)setConfigData:(RevDataGetRechargeIncomeConfig *)configData realData:(RevDataGetTransferInEarn *)realData{
    if(configData!=nil && realData!=nil){
        NSLog(@"level:%@, cur:%@", configData.level, realData.level);
        if([configData.level isEqualToString:realData.level]){
            self.currentLevelLabel.hidden = NO;
        }else{
            self.currentLevelLabel.hidden = YES;
        }
        
        NSDecimalNumber *todayAmountBig = [NSDecimalNumber decimalNumberWithString:realData.todayAmount];
        NSDecimalNumber *nextAmountBig = [NSDecimalNumber decimalNumberWithString:configData.max];
        
        self.currentEarnTodayValueLabel.text = [NSString stringWithFormat:@"%@", realData.todayIncome];
        self.earnRatioValueLabel.text = [NSString stringWithFormat:@"%@", [realData.incomeRate toFinancePercentString]];
        self.withdrawalTimesValueLabel.text = [NSString stringWithFormat:@"%@/%@", realData.remindTimes, realData.totalTimes];
        self.maxProcessValueLabel.text = [NSString stringWithFormat:@"%@", [nextAmountBig stringValue]];
        self.maxSingleTransactionValueLabel.text = [NSString stringWithFormat:@"%@", [todayAmountBig stringValue]];
        
        // 计算进度
        // 检查 nextAmountBig 是否小于等于 0
        if ([nextAmountBig compare:[NSDecimalNumber zero]] != NSOrderedDescending) {
            // 设置进度条的值
            self.process = 0.1;
            
        } else  if ([todayAmountBig compare:[NSDecimalNumber zero]] != NSOrderedDescending) {
            // 设置进度条的值
            self.process = 0.1;
            
        } else  {
            NSDecimalNumber *divBig = [todayAmountBig decimalNumberByDividingBy:nextAmountBig
                                                                   withBehavior:
                                       [NSDecimalNumberHandler
                                        decimalNumberHandlerWithRoundingMode:NSRoundUp
                                        scale:0
                                        raiseOnExactness:NO
                                        raiseOnOverflow:NO
                                        raiseOnUnderflow:NO
                                        raiseOnDivideByZero:YES]];
            // 设置进度条的值
            self.process = [divBig floatValue];
            
            if(self.process>1){
                self.process = 1;
            }
        }
        
        [self setNeedsLayout];
        [self layoutIfNeeded];
    }
}

- (void)toExtAction{
    if(self.actionBlock){
        self.actionBlock();
    }
}

#pragma mark - Getter Setter
YBViewGetter(processContentView, _processContentView, [UIColor hexColor:@"FFFFFF"]);
YBViewGetter(processView, _processView, kThemeColor);

YBImageViewGetter(levelTypeImageView, _levelTypeImageView, [UIImage imageNamed:@"icon_earn_level_1"])

YBLabelGetter(currentLevelLabel, _currentLevelLabel, [UIFont systemFontOfSize:12], @"Current Level", kWhiteColor, NSTextAlignmentCenter);
YBLabelGetter(levelTypeLabel, _levelTypeLabel, [UIFont boldSystemFontOfSize:14], @"Gold", kWhiteColor, NSTextAlignmentCenter);


YBLabelGetter(currentEarnTodayLabel, _currentEarnTodayLabel, [UIFont systemFontOfSize:12], @"Current earn today", kWhiteColor, NSTextAlignmentCenter);
YBLabelGetter(currentEarnTodayValueLabel, _currentEarnTodayValueLabel, [UIFont boldSystemFontOfSize:17], @"*", kWhiteColor, NSTextAlignmentCenter);
YBLabelGetter(currentEarnTodayUnitLabel, _currentEarnTodayUnitLabel, [UIFont systemFontOfSize:10], @"USDT", kWhiteColor, NSTextAlignmentCenter);

YBLabelGetter(earnRatioLabel, _earnRatioLabel, [UIFont systemFontOfSize:12], @"Earn Ratio", kWhiteColor, NSTextAlignmentCenter);
YBLabelGetter(earnRatioValueLabel, _earnRatioValueLabel, [UIFont boldSystemFontOfSize:17], @"*%", kWhiteColor, NSTextAlignmentCenter);

YBLabelGetter(withdrawalTimesLabel, _withdrawalTimesLabel, [UIFont systemFontOfSize:12], @"Withdrawal times", kWhiteColor, NSTextAlignmentCenter);
YBLabelGetter(withdrawalTimesValueLabel, _withdrawalTimesValueLabel, [UIFont boldSystemFontOfSize:17], @"*/*", kWhiteColor, NSTextAlignmentCenter);

YBLabelGetter(maxProcessValueLabel, _maxProcessValueLabel, [UIFont boldSystemFontOfSize:17], @"*", kWhiteColor, NSTextAlignmentCenter);
YBLabelGetter(maxProcessUnitLabel, _maxProcessUnitLabel, [UIFont systemFontOfSize:10], @"USDT", kWhiteColor, NSTextAlignmentCenter);

YBLabelGetter(maxSingleTransactionLabel, _maxSingleTransactionLabel, [UIFont systemFontOfSize:12], @"Max single transaction", kWhiteColor, NSTextAlignmentCenter);
YBLabelGetter(maxSingleTransactionValueLabel, _maxSingleTransactionValueLabel, [UIFont boldSystemFontOfSize:17], @"*", kWhiteColor, NSTextAlignmentCenter);
YBLabelGetter(maxSingleTransactionUnitLabel, _maxSingleTransactionUnitLabel, [UIFont systemFontOfSize:10], @"USDT", kWhiteColor, NSTextAlignmentCenter);

- (UIButton *)actionButton{
    if(!_actionButton){
        _actionButton = [UIButton buttonWithType:UIButtonTypeCustom];
        _actionButton.backgroundColor = kClearColor;
        [_actionButton setTitle:@"" forState:UIControlStateNormal];
        [_actionButton setTitle:@"" forState:UIControlStateSelected];
        [_actionButton setTitleColor:kClearColor forState:UIControlStateNormal];
        [_actionButton setTitleColor:kClearColor forState:UIControlStateSelected];
        [_actionButton addTarget:self action:@selector(toExtAction) forControlEvents:UIControlEventTouchUpInside];
    }
    return  _actionButton;
}
@end
